//index.js
//获取应用实例
var gmapi = require('../../utils/gameapi.js');
const app = getApp()
const innerAudioContext = wx.createInnerAudioContext()
Page({
  data: {
    gqdisplay: 'none',
    gq1display: 'none',
    play_ffno: 'none',
    weappno: 'none',
    motto: '你咋那么的优秀呢~~~',
    musicsrc: app.globalData.bgm1,
    indexplaymuiscno: app.globalData.playmuiscno,
    userInfo: {},
    Checkpoint1: '1',
    k_messages: '进入客服消息后，回复“1”,获取公众号关注链接',
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    classno: 0,
    classleft: 1,
    classright: 1,
    classlist1: {
      "code": "200", //错误   code:519 ,msg:"token过期"
      "msg": "操作成功",
      "data": [{
          "ordernumber": "1", // 关卡序号
          "name": "拼字狂魔", // 称号
          "id": "9a70b0d9800b418796767b84734a180f", // 关卡ID
          "status": 1 // 2 已经解锁 1 可以解锁 0 还未解锁
        },
        {
          "ordernumber": "2",
          "name": "拼字贤者",
          "id": "668474b3c183412693fde22206524d97",
          "status": 0
        },
        {
          "ordernumber": "3",
          "name": "拼字勇者",
          "id": "a86930062bc6443e84edb86270c8c34d",
          "status": 0
        },
        {
          "ordernumber": "4",
          "name": "拼字魔王",
          "id": "5d5fcee4b0954ae7b24c98df4be98c84",
          "status": 0
        },
        {
          "ordernumber": "5",
          "name": "拼字魔神",
          "id": "212776dc7da54b679c4b9d043f0a146e",
          "status": 0
        }
      ],
      "remark": null
    }
  },
  playmuisc: function() {
    var that = this;
    if (app.globalData.playmuiscno == 1) {
      innerAudioContext.pause();
      that.setData({
        indexplaymuiscno: 0
      })
      app.globalData.playmuiscno = 0;
    } else {
      innerAudioContext.play();
      that.setData({
        indexplaymuiscno: 1
      })
      app.globalData.playmuiscno = 1;
    }
  },
  audioPlay: function() {
    this.audioCtx.play()
  },
  audioPause: function() {
    this.audioCtx.pause()
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  minusclass: function() {
    var that = this;
    if (that.data.classno > 0) {
      that.setData({
        classlist: that.data.classmain[that.data.classno - 1],
        classno: that.data.classno - 1
      })
      if (that.data.classno != 0) {
        that.setData({
          classleft: 1
        })
      } else {
        that.setData({
          classleft: 0
        })
      }
      if (that.data.classno == that.data.classmain.length - 1) {
        that.setData({
          classright: 0
        })
      } else {
        that.setData({
          classright: 1
        })
      }
    }

  },
  addclass: function() {
    var that = this;
    if (that.data.classno < that.data.classmain.length) {
      that.setData({
        classlist: that.data.classmain[that.data.classno + 1],
        classno: that.data.classno + 1
      })
      if (that.data.classno != 0) {
        that.setData({
          classleft: 1
        })
      } else {
        that.setData({
          classleft: 0
        })
      }
      if (that.data.classno == that.data.classmain.length - 1) {
        that.setData({
          classright: 0
        })
      } else {
        that.setData({
          classright: 1
        })
      }
    }


  },
  guanqiaclose: function() {
    var that = this;
    that.setData({
      gqdisplay: 'none',
      classno: 0
    })
  },
  guanqia1close: function () {
    var that = this;
    that.setData({
      gq1display: 'none',
      classno: 0
    })
  },
  guanqiaopen: function() {
    var that = this;
    var ordernumber = [];
    //console.log(app.globalData.token);
    wx.request({
      url: app.globalData.urlapi + '/splicingGame/queryGamegradeList',
      method: 'GET',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': app.globalData.token
      },
      success: function(res) {
        if (res.data.code == 200) {
          //console.log(res.data.data)
          var i = 0;
          var x = [];
          var classmain = [];
          var classlist = [];
          //console.log(res.data.data.length / 9)
          while (i < res.data.data.length / 9) {
            for (let j = i * 9; j < res.data.data.length && j < (9 * (i + 1)); j++) {
              x.push(res.data.data[j]);
            }
            classmain.push(x);
            x = [];
            i++;
          }
          classlist = classmain[that.data.classno];
          //console.log(classlist);
          if (res.data.data.length > 9) {
            that.setData({
              classmain,
              classlist,
              classleft: 0
            })
          } else {
            that.setData({
              classmain,
              classlist,
              classleft: 0,
              classright: 0
            })
          }
          that.setData({
            gqdisplay: ''
          })
        } else {
          gmapi.gmgettoken();
        }
      }
    })

  },
  guanqia1open: function () {
    var that = this;
    var ordernumber = [];
    //console.log(app.globalData.token);
    wx.request({
      url: app.globalData.urlapi + '/splicingGame/queryGamegradeList2',
      method: 'GET',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': app.globalData.token
      },
      success: function (res) {
        if (res.data.code == 200) {
          //console.log(res.data)
          var i = 0;
          var x = [];
          var classmain = [];
          var classlist = [];
          //console.log(res.data.data.length / 9)
          while (i < res.data.data.length / 9) {
            for (let j = i * 9; j < res.data.data.length && j < (9 * (i + 1)); j++) {
              x.push(res.data.data[j]);
            }
            classmain.push(x);
            x = [];
            i++;
          }
          classlist = classmain[that.data.classno];
          //console.log(classlist);
          if (res.data.data.length > 9) {
            that.setData({
              classmain,
              classlist,
              classleft: 0
            })
          } else {
            that.setData({
              classmain,
              classlist,
              classleft: 0,
              classright: 0
            })
          }
          that.setData({
            gq1display: ''
          })
        } else {
          gmapi.gmgettoken();
        }
      }
    })

  },
  play_ffopen: function() {
    var that = this;
    that.setData({
      play_ffno: ''
    })
  },
  play_ffcolse: function() {
    var that = this;
    that.setData({
      play_ffno: 'none'
    })
  },
  goRule:function(){
    wx.navigateTo({
      url: '../../pages/rule/index'
    })
  },
  goAbout:function(){
    wx.navigateTo({
      url: '../../pages/about/index'
    })
  },
  weappopen: function() {
    var that = this;
    that.setData({
      weappno: ''
    })
  },
  weappclose: function() {
    var that = this;
    that.setData({
      weappno: 'none'
    })
  },
  goselectlist: function() {
    wx.navigateTo({
      url: '../select/select_list'
    });
  },
  goplay(e) {
    var that = this;
    //console.log(e.target.dataset.status + ':' + e.target.dataset.ordernumber)

    if (e.target.dataset.status == 0) {

    } else {

      wx.navigateTo({
        url: '../game/play?id=' + e.target.dataset.ordernumber
      });
      that.setData({
        gqdisplay: 'none'
      })
    }
  },
  goplaygame2(e) {
    var that = this;
    //console.log(e.target.dataset.status + ':' + e.target.dataset.ordernumber)

    if (e.target.dataset.status == 0) {

    } else {

      wx.navigateTo({
        url: '../game2/play?id=' + e.target.dataset.ordernumber
      });
      that.setData({
        gq1display: 'none'
      })
    }
  },
  bindGetUserInfo(e) {
    var that = this;
    if (e.detail.iv == null) {
      // console.log('未授权');
    } else {
      if (app.globalData.token == null) {
        wx.request({
          url: 'https://api.wnymeng.com/wxmini/userinfo',
          method: 'POST',
          data: {
            session_key: app.globalData.session_key,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
          },
          success: function(res) {
            if (res.data.code == 200) {
              // console.log('授权成功')
              app.globalData.token = res.data.data;
              that.guanqiaopen();
            } else {
              console.log('授权失败')
            }
          }
        })
      } else {
        that.guanqiaopen();
      }
    }
  },
  bindGetUserInfo1(e) {
    var that = this;
    if (e.detail.iv == null) {
      // console.log('未授权');
    } else {
      if (app.globalData.token == null) {
        wx.request({
          url: 'https://api.wnymeng.com/wxmini/userinfo',
          method: 'POST',
          data: {
            session_key: app.globalData.session_key,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
          },
          success: function (res) {
            if (res.data.code == 200) {
              // console.log('授权成功')
              app.globalData.token = res.data.data;
              that.guanqia1open();
            } else {
              console.log('授权失败')
            }
          }
        })
      } else {
        that.guanqia1open();
      }
    }
  },
  onLoad: function() {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onReady: function(e) {

    innerAudioContext.autoplay = false
    innerAudioContext.loop = true
    innerAudioContext.src = 'http://static.wnymeng.com/upload/images/SplicingGameBGM/1.mp3'
    innerAudioContext.onPlay(() => {
      // console.log('开始播放')
    })
    innerAudioContext.onPause(() => {
      //console.log('暂停播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
    if (innerAudioContext != null && app.globalData.playmuiscno == 1) {
      // console.log("onReady 开始播放" + app.globalData.playmuiscno)

      innerAudioContext.play()
    }
    var that = this;
    that.setData({
      indexplaymuiscno: app.globalData.playmuiscno
    })
  },
  onHide: function() {
    var that = this;
    //console.log("onHide:" + app.globalData.playmuiscno)
    innerAudioContext.pause();
    //this.audioCtx.pause();
    that.weappclose();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    if (innerAudioContext != null && app.globalData.playmuiscno == 1) {
      console.log("onshow重新播放" + app.globalData.playmuiscno)

      innerAudioContext.play()
    }
    //this.countDown(9);
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

    innerAudioContext.pause();
  },
  getUserInfo: function(e) {
    //console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  /**
   * 用户点击分享
   */
  onShareAppMessage: function() {
    path: '/page/user?id=123'
  }
})